<template>
  <div class="bottom-bar">
    <div class="check-content">
      <check-button
        :is-checked="isSelectAll"
        class="check-button"
        @click.native="checkClick"
      ></check-button>
      <span>全选</span>
    </div>

    <div class="price">合计:{{ totalPrice }}</div>

    <div class="calculate" @click="calcClick">去结算({{ checkLength }})</div>
  </div>
</template>

<script>
import CheckButton from "../../../components/content/checkButton/CheckButton.vue";

import { mapGetters } from "vuex";

export default {
  name: "CartBottomBar",
  components: {
    CheckButton,
  },
  data() {
    return {};
  },
  computed: {
    ...mapGetters(["cartList"]),
    totalPrice() {
      return (
        "￥" +
        this.cartList
          .filter((item) => {
            return item.checked;
          })
          .reduce((preValue, item) => {
            return preValue + item.price * item.count;
          }, 0)
          .toFixed(2)
      );
    },
    checkLength() {
      return this.cartList.filter((item) => {
        return item.checked;
      }).length;
    },
    isSelectAll() {
      // return !this.cartList.filter((item) => !item.checked).length;
      if (this.cartList.length === 0) {
        return false;
      } else {
        return !this.cartList.find((item) => !item.checked);
      }
    },
  },
  methods: {
    checkClick() {
      if (this.isSelectAll) {
        this.cartList.forEach((item) => {
          item.checked = false;
        });
      } else {
        this.cartList.forEach((item) => {
          item.checked = true;
        });
      }
    },
    calcClick() {
      if (!this.cartList.find((item) => item.checked)) {
        this.$toast.show("请选择您的商品", 1500);
      }
    },
  },
};
</script>

<style scoped>
.bottom-bar {
  height: 40px;
  background-color: #eee;
  position: relative;
  display: flex;
  align-items: center;
}
.check-content {
  display: flex;
  align-items: center;
  margin: 0 0 0 10px;
  width: 60px;
}
.check-button {
  width: 20px;
  height: 20px;
  margin: 0 5px 0 0;
}
.price {
  margin-left: 15px;
  flex: 1;
}
.calculate {
  width: 120px;
  height: 40px;
  background-color: rgb(252, 149, 31);
  line-height: 40px;
  color: #fff;
  font-size: 20px;
  padding-left: 20px;
}
</style>